
//
// 120425 - AH - Created.
// 120507 - AH - Get parameter value with default values.
// 130312 - AH - FetchParameterResult(int).
//

package org.japril.servlet;

import org.japril.servlet.HttpRequestParamDef;

/**
 * <p>
 *   Represents the result of fetching a parameter from a
 *   {@link HttpRequestParamDef} using
 *   {@link HttpRequestParamDef#getValue(HttpServletRequest)}.
 * </p>
 * @author Arne Halvorsen (AH)
 */
public final class FetchParameterResult
{
  private final boolean m_hasValue;
  
  private String m_msg=null;
  
  private String m_sValue=null;
  
  private Long m_lvalue=null;
  
  private Boolean m_bvalue=null;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   */
  FetchParameterResult(){ m_hasValue=false; }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param err Error if {@code true} {@code v} is error message else is 
   *            parameter value. 
   * @param v   Error message or value.
   */
  FetchParameterResult(boolean err, String v)
  { 
    if (err)
    {
      m_msg=v;
      m_hasValue=false;
    }
    else     
    {
      m_hasValue=true;
      m_sValue=v;
    }
  }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param v Value.
   */
  FetchParameterResult(String v){  m_hasValue=true; m_sValue=v; }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param v Value.
   */
  FetchParameterResult(int v)
  { 
    m_hasValue=true;
    m_lvalue=new Long(v); 
    m_sValue=Long.toString(v); 
  }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param v Value.
   */
  FetchParameterResult(long v)
  { 
    m_hasValue=true;
    m_lvalue=v; 
    m_sValue=Long.toString(v); 
  }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param v Value.
   */
  FetchParameterResult(boolean v)
  {
    m_hasValue=true;
    m_bvalue=v;
    m_sValue=v ? "true" : "false";
  }
  
  /**
   * <p>
   *   Tells if error occurred during parsing.
   * </p>
   * @return {@code true} if did.
   */
  public boolean error(){ return m_msg!=null; }
  
  /**
   * <p>
   *   Gets error message.
   * </p>
   * @return Error message or {@code null} if 
   *         {@code error()==false}.
   */
  public String errorMsg(){ return m_msg; }
  
  /**
   * <p>
   *   Tells if has value.
   * </p>
   * <p>
   *   Can be {@code false} if parameter is optional or there was an error.
   * </p>
   * @return {@code true} if has value, {@code false} if has not.
   */
  public boolean hasValue(){ return m_hasValue; }
  
  /**
   * <p>
   *   Gets
   *   {@link String} value.
   * </p>
   * @return Value or {@code null} if 
   *         {@code error()!=null}.
   */
  public String getStringValue(){ return m_sValue; }
  
  /**
   * <p>
   *   Gets
   *   {@link String} value.
   * </p>
   * @param Default value to return if is optional and not given.
   * @return Value or {@code null} if 
   *         {@code error()!=null}.
   */
  public String getStringValue(String def)
  {
    if (m_msg!=null) return null;
    return m_sValue==null ? def : m_sValue; 
  }
  
  /**
   * <p>
   *   Gets 
   *   {@code Long} value.
   * </p>
   * @return {@code Long} value or {@code null} if either does not have 
   *         {@code long} presentation or
   *         {@code error()!=null}.
   */
  public Long getLongValue(){ return m_lvalue; }
  
  /**
   * <p>
   *   Gets 
   *   {@code Long} value.
   * </p>
   * @param Default value.
   * @return {@code Long} value or {@code null} if {@code error()!=null}.
   */
  public Long getLongValue(Long def)
  {
    if (m_msg!=null) return null;
    return m_lvalue==null ? def : m_lvalue; 
  }
  
  /**
   * <p>
   *   Gets 
   *   {@code Boolean} value.
   * </p>
   * @return {@code Long} value or {@code null} if either does not have 
   *         {@code boolean} presentation or
   *         {@code error()!=null}.
   */
  public Boolean getBooleanValue(){ return m_bvalue; }
  
  /**
   * <p>
   *   Gets 
   *   {@code Boolean} value.
   * </p>
   * @return {@code Boolean} value or {@code null} if {@code error()!=null}.
   */
  public Boolean getBooleanValue(Boolean def)
  { 
    if (m_msg!=null) return null;
    return m_bvalue==null ? def : m_bvalue; 
  }
  
}
